/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss.cms.action;

import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

import com.jinhe.tss.cms.CMSConstants;
import com.jinhe.tss.cms.entity.ArticleType;
import com.jinhe.tss.cms.service.IArticleTypeService;
import com.jinhe.tss.component.dynproperty.support.IRemotePropertyService;
import com.jinhe.tss.component.support.web.webwork.PTActionSupport;
import com.jinhe.tss.core.exception.BusinessException;
import com.jinhe.tss.core.util.EasyUtils;
import com.jinhe.tss.core.util.XMLDocUtil;
import com.jinhe.tss.core.web.dispaly.tree.LevelTreeParser;
import com.jinhe.tss.core.web.dispaly.tree.TreeEncoder;
import com.jinhe.tss.core.web.dispaly.xform.XFormEncoder;

public class ArticleTypeAction extends PTActionSupport {

    private static final long serialVersionUID = 6763008022306015845L;

    private ArticleType articleType = new ArticleType();
    
    private IArticleTypeService service;
    private IRemotePropertyService propertyService;

    private Long typeId;
    private Long groupId;
 
    /**
     * 获取所有文章种类组
     */
    public String getAllArticleTypeGroup() {
        List<?> list = service.getArticleTypeGroups();
        TreeEncoder encoder = new TreeEncoder(list, new LevelTreeParser());
        encoder.setRootCanSelect(false);
        return print("ArticleTypeTree", encoder);
    }
    
    /**
     * 文章类型管理模块的文章类型树结构
     */
    public String getAllArticleTypeTree() {
        TreeEncoder encoder = new TreeEncoder(service.getArticleTypes(), new LevelTreeParser());
        return print("ArticleTypeTree", encoder);
    }

    /**
     * 创建栏目时，选择文章类型树结构
     */
    public String getArticleTypeTree4Channel() {
        TreeEncoder encoder = new TreeEncoder(service.getArticleTypes(), new LevelTreeParser());
        encoder.setRootCanSelect(false);
        return print("ArticleTypeTree", encoder);
    }

    /**
     * 初始化文章种类组新增信息
     */
    public String initArticleTypeGroup() {
        ArticleType articleType = new ArticleType();
        articleType.setParentId(CMSConstants.HEAD_NODE_ID);
        articleType.setIsGroup(CMSConstants.TRUE);
        return print("ArticleTypeGroupInfo", new XFormEncoder(CMSConstants.XFORM_TEMPLATE_ARTICLETYPEGROUP, articleType));
    }

    /**
     * 获取文章种类组信息
     */
    public String getArticleTypeGroup() {
        ArticleType type = service.getArticleType(groupId);
        return print("ArticleTypeGroupInfo", new XFormEncoder(CMSConstants.XFORM_TEMPLATE_ARTICLETYPEGROUP, type));
    }

    /**
     * 初始化文章种类新增信息
     */
    public String initArticleTypeInfo() {
        ArticleType articleType = new ArticleType();
        articleType.setParentId(groupId);
        articleType.setIsGroup(CMSConstants.FALSE);
        return print("ArticleTypeInfo", new XFormEncoder(CMSConstants.XFORM_TEMPLATE_ARTICLETYPE, articleType));
    }

    /**
     * 获取文章种类信息
     */
    public String getArticleTypeInfo() {
        ArticleType type = service.getArticleType(typeId);
        return print("ArticleTypeInfo", new XFormEncoder(CMSConstants.XFORM_TEMPLATE_ARTICLETYPE, type));
    }

    /**
     * 保存文章种类/文章种类组信息
     */
    public String saveArticleType() {
        articleType = service.createArticleType(articleType);
        if (!articleType.isTypeGroup()) {
            checkClasspath(articleType.getPublishArticleClassName());
            
            //将文章的模板导入到动态属性实体中
            Document doc = XMLDocUtil.createDoc(CMSConstants.TEMPLATE_ARTICLE_PROPERTY);
            Element root = doc.getRootElement();
            root.addAttribute("name", articleType.getName());
            root.addAttribute("code", articleType.getEntityCode());
            propertyService.importDynableEntity(doc.asXML());
        }
        return doAfterSave(true, articleType, "ArticleTypeTree");
    }
    
    /**
     * 验证类路径是否正确（错误的话会抛出异常）
     */
    private void checkClasspath(String classpath){
        if( EasyUtils.isNullOrEmpty(classpath) ) return;
        
        try {
            ClassLoader.getSystemClassLoader().loadClass(classpath);
        } catch (Exception e) {
            throw new BusinessException("发布文章实现类的全路径错误！", e);
        }    
    }

    /**
     * 修改文章种类/文章种类组信息
     */
    public String updateArticleType() {
        service.updateArticleType(articleType);
        if (!articleType.isTypeGroup()) {
            checkClasspath(articleType.getPublishArticleClassName());
            
            //更新文章类型对应的动态实体名称
            propertyService.updateDynableEntityName(articleType.getEntityCode(), articleType.getName());
        }
        return printSuccessMessage();
    }

    /**
     * 删除文章种类信息
     */
    public String delArticleTypeInfo() {
        ArticleType type = service.deleteArticleType(typeId);
        propertyService.deleteDynableEntity(type.getEntityCode());
        return printSuccessMessage("删除成功！");
    }

    /**
     * 删除文章种类组
     * @return
     */
    public String delArticleTypeGroup() {
        service.deleteArticleTypeGroup(groupId);
        return printSuccessMessage("删除成功！");
    }

    /**
     * 移动文章种类
     */
    public String moveArticleType() {
        ArticleType type = service.getArticleType(typeId);
        type.setParentId(groupId);
        service.updateArticleType(type);
        
        return printSuccessMessage("移动成功！");
    }
    
    public ArticleType getArticleType() {
        return articleType;
    }
 
    public void setGroupId(Long groupId) {
        this.groupId = groupId;
    }
 
    public void setService(IArticleTypeService service) {
        this.service = service;
    }
 
    public void setTypeId(Long typeId) {
        this.typeId = typeId;
    }
    
    public void setPropertyService(IRemotePropertyService propertyService) { 
    	this.propertyService = propertyService; 
    }
}
